"""
generate_sample.py 生成测试用Excel文件
"""
from openpyxl import Workbook
from openpyxl.styles import PatternFill
import random
import os

red_fill = PatternFill(start_color='FFC7CE', end_color='FFC7CE', fill_type='solid')

def create_valid_points():
    """创建合法点位表示例"""
    wb = Workbook()
    ws = wb.active
    
    # 点位行
    ws.append(['S', 'F', '11', '22', '33', '44', '55'])
    # 分值行
    ws.append(['', '', 1, 2, 3, 4, 5])
    
    wb.save(os.path.join('tests', 'test_data', 'valid_points.xlsx'))

def create_invalid_sf():
    """创建S/F位置错误的示例"""
    wb = Workbook()
    ws = wb.active
    
    # 错误的S/F顺序
    ws.append(['F', 'S', '11', '22'])
    ws.append(['', '', 1, 2])
    
    # 标记错误单元格
    ws['A1'].fill = red_fill
    ws['B1'].fill = red_fill
    
    wb.save(os.path.join('tests', 'test_data', 'invalid_sf.xlsx'))

def create_valid_matrix(points):
    """创建合法耗时矩阵"""
    wb = Workbook()
    ws = wb.active
    
    # 表头行
    ws.append([''] + points)
    
    # 数据行
    for start in points:
        row = [start]
        for end in points:
            if start == end:
                row.append(0.0)
            elif start == 'F' or end == 'S':
                row.append(0.0)
            else:
                row.append(round(random.uniform(1.0, 5.0), 1))
        ws.append(row)
    
    wb.save(os.path.join('tests', 'test_data', 'valid_matrix.xlsx'))

def create_invalid_matrix():
    """创建行列不一致的矩阵"""
    wb = Workbook()
    ws = wb.active
    points = ['S', 'F', '11', '22']
    
    # 表头有4个点（不含空单元格）
    ws.append([''] + points)
    
    # 数据行只有3个点且少一列
    for start in points[:3]:
        row = [start] + [round(random.uniform(1.0, 5.0), 1) for _ in range(3)]
        ws.append(row)
    
    # 标记错误行
    for cell in ws[4]:
        cell.fill = red_fill
    
    wb.save(os.path.join('tests', 'test_data', 'invalid_matrix.xlsx'))

def create_duplicate_points():
    """创建重复点位标识"""
    wb = Workbook()
    ws = wb.active
    
    # 重复的11点位
    ws.append(['S', 'F', '11', '11', '22'])
    ws.append(['', '', 1, 1, 2])
    
    # 标记重复单元格
    ws['C1'].fill = red_fill
    ws['D1'].fill = red_fill
    
    wb.save('tests/test_data/duplicate_points.xlsx')

def create_invalid_control():
    """创建无效控制点格式"""
    wb = Workbook()
    ws = wb.active
    
    # 包含字母的控制点
    ws.append(['S', 'F', 'A1', 'B2'])
    ws.append(['', '', 1, 2])
    
    # 标记无效单元格
    ws['C1'].fill = red_fill
    ws['D1'].fill = red_fill
    
    wb.save('tests/test_data/invalid_control.xlsx')

def create_invalid_score():
    """创建无效分值"""
    wb = Workbook()
    ws = wb.active
    
    # 包含非整数的分值
    ws.append(['S', 'F', '11', '22'])
    ws.append(['', '', 1.5, '二'])
    
    # 标记错误单元格
    ws['C2'].fill = red_fill
    ws['D2'].fill = red_fill
    
    wb.save('tests/test_data/invalid_score.xlsx')

def create_invalid_time():
    """创建时间格式错误"""
    wb = Workbook()
    ws = wb.active
    points = ['S', 'F', '11', '22']
    
    ws.append([''] + points)
    # Only modify non-F/S rows and columns
    for start in points[2:]:  # Skip S/F
        row = [start]
        for end in points[2:]:  # Skip S/F
            if start == end:
                row.append(0.0)
            else:
                row.append(-1.5 if random.random() > 0.5 else 'N/A')
        # Fill remaining columns with 0.0 for S/F
        row += [0.0] * 2
        ws.append(row)
    
    # 标记错误单元格
    for row in ws.iter_rows(min_row=2):
        for cell in row[1:]:
            if cell.value in (-1.5, 'N/A'):
                cell.fill = red_fill
    
    wb.save('tests/test_data/invalid_time.xlsx')

if __name__ == '__main__':
    points = ['S', 'F', '11', '22', '33', '44', '55']
    create_valid_points()
    create_invalid_sf()
    create_valid_matrix(points)
    create_invalid_matrix()
    create_duplicate_points()
    create_invalid_control()
    create_invalid_score()
    create_invalid_time()
